package com.itcast.spring6.aop.api;

import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 环绕通知
 * @author zhangtian105
 * @date 2023/5/11
 * @apiNote
 */
@Slf4j
@Component(value = "myMethodInterceptor")
public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object target = invocation.getThis();
        Method method = invocation.getMethod();
        Object[] args = invocation.getArguments();

        String targetName = target.getClass().getName();
        String methodName = method.getName();
        String params = Arrays.toString(args);
        String msg = "【环绕通知】：正在调用->" + targetName + "." + methodName + ",携带的参数：" + params;
        log.info("【Around】spring6.MyMethodInterceptor.invoke: {}", msg);
        //invocation可以执行被代理的目标对象的业务方法
        //过滤器    chain.dofilter:放行
        Object returnValue = invocation.proceed();
        String msg2 = "【环绕通知】：目标对象所调用的方法的返回值：" + returnValue;
        log.info("【Around】spring6.MyMethodInterceptor.invoke return: {}", msg2);

        return returnValue;
    }
}
